package grp.test.service.Impl;

import grp.test.common.ResponseCode;
import grp.test.common.ServerResponse;
import grp.test.dao.BookMapper;
import grp.test.pojo.Book;
import grp.test.service.ISearchSeverice;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;



/**
 * Created by admin on 2020/8/15.
 */
@Service("iSearchService")
public class ISearchSevericeImpl implements ISearchSeverice {
    @Autowired(required=false)
    BookMapper bookMapper;
    String bookname=null;
    String des=null;
    public ServerResponse<List<Book>> getBookList(String bookname,String des){
        List<Book> bookList=bookMapper.getBookListDes(des);
        List<Book> bookList1=bookMapper.getBookListName(bookname);
        List<Book> searchList=new ArrayList<>();
        if (bookList.size()==0&&bookList1.size()==0) {
            return ServerResponse.createByErrorMessage("未搜索到任何书籍");
        }
        else if(bookList1.size()==0 && bookList.size()!=0)
        {
            for (int  i=0;i<bookList.size();i++){
                searchList.add(bookMapper.selectByPrimaryKey(bookList.get(i).getBookId()));
            }
            return ServerResponse.createBySuccess("读取到数据",searchList);
        }
        else if (bookList1.size()!=0 && bookList.size()==0)
        {
            for (int j=0;j<bookList1.size();j++)
            {
                searchList.add(bookMapper.selectByPrimaryKey(bookList1.get(j).getBookId()));
            }
            return ServerResponse.createBySuccess("读取到"+searchList.size()+"数据",searchList);
        }
        else
        {
            ArrayList<Integer> array=new ArrayList<Integer>();

            for (int m=0; m<bookList.size();m++)
            {
                array.add(bookList.get(m).getBookId());
                searchList.add(bookMapper.selectByPrimaryKey(bookList.get(m).getBookId()));

            }
            for (int n=0;n<bookList1.size();n++)
            {
               if (array.contains(bookList1.get(n).getBookId()))
               {
                   continue;
               }
                else{
                   searchList.add(bookMapper.selectByPrimaryKey(bookList1.get(n).getBookId()));
               }
            }
            return ServerResponse.createBySuccess("读取到"+searchList.size()+"数据",searchList);

        }
    }
    @Override
    public ServerResponse<List<Book>> getBookListDes(String des){
        List<Book> bookList=bookMapper.getBookListDes(des);
        if (bookList.size()==0){
            return ServerResponse.createByErrorMessage("未搜索到任何书籍");
        }
        List<Book> searchList=new ArrayList<>();
        for (int  i=0;i<bookList.size();i++){
            searchList.add(bookMapper.selectByPrimaryKey(bookList.get(i).getBookId()));
        }
        return ServerResponse.createBySuccess("读取到"+searchList.size()+"条数据",searchList);
    }

    @Override
    public ServerResponse<List<Book>> getBookListName(String bookName) {
        List<Book> bookListName = bookMapper.getBookListName(bookName);
        if (bookListName.size()==0){
            return ServerResponse.createByErrorMessage("未搜索到任何书籍");
        }
        List<Book> searchList=new ArrayList<>();
        for (int  i=0;i<bookListName.size();i++){
            searchList.add(bookMapper.selectByPrimaryKey(bookListName.get(i).getBookId()));
        }
        return ServerResponse.createBySuccess("读取到"+searchList.size()+"条数据",searchList);
    }
    @Override
    public ServerResponse<Book> getBook(Integer bookId) {
        Book book = bookMapper.selectByPrimaryKey(bookId);
        if (book == null) {
            return ServerResponse.createByErrorCodeMessage(ResponseCode.ERROR.getCode(), "没有搜索到任何信息");
        }
        return ServerResponse.createBySuccess("数据读取成功",book);
    }
}
